package com.flydog.mall.controller.pms;

import com.flydog.mall.common.api.CommonResult;
import com.flydog.mall.common.api.PageResponse;
import com.flydog.mall.dto.PmsProductAttributeParam;
import com.flydog.mall.entity.pms.PmsProductAttribute;
import com.flydog.mall.service.pms.PmsProductAttributeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Cao
 * @description 商品属性
 * @create 2021-03-24
 */
@RestController
@Slf4j
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {

    @Autowired
    private PmsProductAttributeService attributeService;

    @GetMapping(value = "/list/{id}")
    CommonResult<PageResponse<PmsProductAttribute>>
    getListByCid(@PathVariable("id") Long cid,
                 @RequestParam("pageNum") Integer pageNum,
                 @RequestParam("pageSize") Integer pageSize,
                 @RequestParam("type") Integer type) {
        return CommonResult.success(attributeService.getListByCidAndType(cid, type, pageNum, pageSize));
    }

    @PostMapping(value = "/create")
    CommonResult<Integer> create(@RequestBody PmsProductAttributeParam attribute){
        return CommonResult.success(attributeService.create(attribute));
    }

    @PostMapping(value = "/update/{id}")
    CommonResult<Integer> update(@PathVariable("id") Long id, @RequestBody PmsProductAttributeParam param){
        return CommonResult.success(attributeService.update(id, param));
    }

    @GetMapping(value = "/{id}")
    CommonResult<PmsProductAttribute> getById(@PathVariable("id") Long id){
        return attributeService.getById(id).map(CommonResult::success).orElseGet(CommonResult::failed);
    }

    @PostMapping(value = "delete")
    CommonResult<Integer> deleteById(@RequestParam("ids") Long[] ids) {
        return CommonResult.success(attributeService.delete(ids));
    }
}
